<?php

namespace Home\Controller;

/**
 * 游戏管理-抽奖
 *
 * @author Administrator
 *        
 */
class CJController extends BaseController {
	/**
	 *
	 * @var \Req_sfsCJ
	 */
	private $m_ReqBJL;
	/**
	 *
	 * @var integer
	 */
	private $m_GameID = 15;
	/**
	 */
	function __construct() {
		$this->m_ReqBJL = new \Req_sfsCJ ();
	}
	/**
	 * 获取配置
	 */
	public function mb_GetConfig() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->GetConfig () );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		
		$data = $reqData->data;
		$conn = $data->JTTX;
		$name = $data->FJM;
		
		$gameInfo = M ( 'Gameinfo' );
		$gameData = $gameInfo->where ( "id='$this->m_GameID'" )->find ();
		$config = array (
				'game' => $gameData ['status'],
				'conn' => ( string ) $conn,
				'name' => ( string ) $name 
		);
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $config ) );
	}
	/**
	 * 设置配置
	 */
	public function mb_SetConfig() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$game = $data->game;
		$conn = $data->conn;
		$name = $data->name;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$gameInfo = M ( 'Gameinfo' );
		$gameInfo->save ( array (
				'id' => $this->m_GameID,
				'status' => $game 
		) );
		
		$lobbyAppInfo = M ( 'Appinfo', '', 'DB_LOBBY' );
		$lobbyAppInfo->save ( array (
				'id' => $this->m_GameID,
				'status' => $game 
		) );
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->SetConfig ( $conn, $name, $game ) );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		
		\WMHandler::mb_GetInstance ()->mb_UpdateGameStatus ( $this->m_GameID, $game );
		
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 获取奖项配置
	 */
	public function mb_GetAwardsConfig() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		if ($adminData ['level'] != 0) {
			$this->mb_OutMessage ( ErrCode_Toast, '权限不足' );
			return;
		}
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->GetAwardsConfig () );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		
		$send = array ();
		$data = $reqData->data->JXSZ;
		
		for($i = 0; $i < count ( $data ); $i ++) {
			$tmp = array (
					'id' => ( string ) $data [$i]->ID, // ID
					'name' => ( string ) $data [$i]->JXM, // 奖项
					'value' => ( string ) $data [$i]->JZ, // 奖值
					'count' => ( string ) $data [$i]->HJCS  // 获奖次数
			);
			$send [] = $tmp;
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $send ) );
	}
	/**
	 * 设置配置
	 */
	public function mb_SetAwardsConfig() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$id = $data->id;
		$name = $data->name;
		$value = $data->value;
		$count = $data->count;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->SetAwardsConfig ( $id, $name, $value, $count ) );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, '设置成功' );
	}
	/**
	 * 添加奖项
	 */
	public function mb_AddConfig() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$id = $data->id;
		$name = $data->name;
		$value = $data->value;
		$count = $data->count;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->AddConfig ( $id, $name, $value, $count ) );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, '添加成功' );
	}
	/**
	 * 删除奖项
	 */
	public function mb_DeleteConfig() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$id = $data->id;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->DeleteConfig ( $id ) );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, '删除成功' );
	}
	/**
	 * 获取中奖明细
	 */
	public function mb_GetAwardsDetail() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$type = $data->type;
		$key = $data->key;
		$start = $data->start;
		$end = $data->end;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->GetAwardsDetail ( $type, $key, $start, $end ) );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		
		$data = $reqData->data->YZMS;
		$send = array ();
		for($i = 0; $i < count ( $data ); $i ++) {
			$obj = $data [$i];
			$tmp = array (
					'id' => ( string ) $obj->UID,
					'name' => ( string ) $obj->WJZH,
					'seatid' => ( string ) $obj->ZW,
					'awardname' => ( string ) $obj->JXM,
					'before' => ( string ) $obj->JFQ,
					'awardvalue' => ( string ) $obj->JZ,
					'point' => ( string ) $obj->JFH,
					'date' => ( string ) $obj->RQ 
			);
			$send [] = $tmp;
		}
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $send ) );
	}
	/**
	 * 获取房间信息
	 */
	public function mb_GetRoomInfo() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->GetRoomInfo () );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		$seats = array ();
		
		$data = $reqData->data->FJXX;
		for($i = 0; $i < count ( $data ); $i ++) {
			$obj = $data [$i];
			$tmp = array (
					'id' => ( string ) $obj->PID,
					'seatid' => ( string ) $obj->ZW,
					'point' => ( string ) $obj->JB,
					'code' => ( string ) $obj->YQM 
			);
			$seats [] = $tmp;
		}
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $seats ) );
	}
	/**
	 * 抽奖
	 */
	public function mb_LotteryAwards() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$awardId = $data->award;
		$seatId = $data->seat;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->LotteryAwards ( $awardId, $seatId ) );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		$this->mb_OutMessage ( ErrCode_Normal, $reqData->msg );
	}
	/**
	 * 开始抽奖
	 */
	public function mb_BeginLottery() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->BeginLottery () );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		$this->mb_OutMessage ( ErrCode_Normal, '通知成功' );
	}
	/**
	 * 停止抽奖
	 */
	public function mb_StopLottery() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$seatId = $data->seat;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->StopLottery ( $seatId ) );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		$this->mb_OutMessage ( ErrCode_Normal, '通知成功' );
	}
	/**
	 * 获取申请列表
	 */
	public function mb_GetApplyList() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->GetApplyList () );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		$data = $reqData->data->JXSZ;
		
		$send = array ();
		for($i = 0; $i < count ( $data ); $i ++) {
			$tmp = array (
					'id' => ( string ) $data [$i]->WJID,
					'name' => ( string ) $data [$i]->WJZH,
					'coin' => ( string ) $data [$i]->WJFS,
					'code' => ( string ) $data [$i]->WJYQM,
					'date' => ( string ) $data [$i]->SQSJ 
			);
			$send [] = $tmp;
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $send ) );
	}
	/**
	 */
	public function mb_HandleApplyMsg() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$id = $data->id;
		$result = $data->result;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->HandleApplyMsg ( $id, $result ) );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, '处理成功' );
	}
	/**
	 */
	public function mb_GetAwardsList() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->GetAwardsList () );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		
		$send = array ();
		$data = $reqData->data->JXSZ;
		
		for($i = 0; $i < count ( $data ); $i ++) {
			$tmp = array (
					'id' => ( string ) $data [$i]->ID, // ID
					'name' => ( string ) $data [$i]->JXM, // 奖项
					'value' => ( string ) $data [$i]->JZ, // 奖值
					'count' => ( string ) $data [$i]->HJCS, // 获奖次数
					'bewin' => ( string ) $data [$i]->YHJ  // 已获奖
			);
			$send [] = $tmp;
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $send ) );
	}
	public function mb_GetLotteryList() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->GetLotteryList () );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		
		$send = array ();
		$data = $reqData->data->CJMD;
		
		for($i = 0; $i < count ( $data ); $i ++) {
			$tmp = array (
					'id' => ( string ) $data [$i]->WJID, // ID
					'name' => ( string ) $data [$i]->WJZH, // 奖项
					'code' => ( string ) $data [$i]->YQM  // 奖值
			);
			$send [] = $tmp;
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $send ) );
	}
	public function mb_AddLotteryPlayer() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$id = $data->id;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->AddLotteryPlayer ( $id ) );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, $reqData->msg );
	}
	public function mb_RemoveLotteryPlayer() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$id = $data->id;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->RemoveLotteryPlayer ( $id ) );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, $reqData->msg );
	}
	/**
	 */
	public function mb_GetTodayLotteryAward() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->GetTodayLotteryAward () );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		
		$data = $reqData->data->YZMS;
		$send = array ();
		for($i = 0; $i < count ( $data ); $i ++) {
			$obj = $data [$i];
			$tmp = array (
					'id' => ( string ) $obj->UID,
					'name' => ( string ) $obj->WJZH,
					'seatid' => ( string ) $obj->ZW,
					'awardname' => ( string ) $obj->JXM,
					'before' => ( string ) $obj->JFQ,
					'awardvalue' => ( string ) $obj->JZ,
					'point' => ( string ) $obj->JFH,
					'date' => ( string ) $obj->RQ 
			);
			$send [] = $tmp;
		}
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $send ) );
	}
	public function mb_GetAllSendOut() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->AllSendOut () );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		
		$send = ( string ) $reqData->data->ZSC;
		$this->mb_OutMessage ( ErrCode_Normal, $send );
	}
	/**
	 * 统计抽奖名单
	 */
	public function mb_StatisticsLotteryList() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$start = $data->start;
		$end = $data->end;
		$coins = $data->coins;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$totalData = array ();
		$logInfo = M ( 'Userdatalog' );
		$logData = $logInfo->where ( "date >= '$start' AND date <= '$end'" )->getField ( 'name,coins', true );
		
		foreach ( $logData as $key => $value ) {
			$name = $key;
			$coin = $value;
			if (isset ( $totalData [$name] )) {
				$totalData [$name] += $coin;
			} else {
				$totalData [$name] = $coin;
			}
		}
		
		$names = array ();
		foreach ( $totalData as $key => $value ) {
			if (abs ( $value ) >= $coins) {
				$names [] = $key;
			}
		}
		
		$reqData = $this->mb_DecodeSFSData ( $this->m_ReqBJL->StatisticsLotteryList ( $names ) );
		if ($reqData->state == false) {
			$this->mb_OutMessage ( ErrCode_Toast, $reqData->msg );
			return;
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $names ) );
	}
	/**
	 *
	 * @param unknown $reqData        	
	 * @return \stdClass
	 */
	private function mb_DecodeSFSData($reqData) {
		$data = new \stdClass ();
		if ($reqData == null || $reqData == '') {
			$data->state = false;
			$data->msg = '外部服务器异常，请稍后再试';
			return $data;
		}
		$reqJson = json_decode ( $reqData );
		if (json_last_error () != JSON_ERROR_NONE) {
			$data->state = false;
			$data->msg = '外部服务器异常，请稍后再试';
			return $data;
		}
		$state = $reqJson->state;
		if ($state == false) {
			$data->state = false;
			$data->msg = $reqJson->msg;
			return $data;
		}
		$data->state = true;
		if (isset ( $reqJson->msg ))
			$data->msg = $reqJson->msg;
		$data->data = $reqJson;
		return $data;
	}
}